﻿--[[
   @brief    GroupBannerList
   @author   vincentlao
   @version  1.0
   @date     2015-06-20
]]

ierd_tgp.tpf_ui.Require("social\\group_list\\group_list.lua");
ierd_tgp.tpf_ui.Require("social\\group_list\\group_guide.lua");
ierd_tgp.tpf_ui.Require("social\\talk_dialog\\control.lua");

group_list_view = 
{
    root_id_ = 0;
    uilib_ = nil;
    inited_ = false;

    show = function()
        if group_list_view.inited_ then
            return;
        end

        base_utility.log("group_list init");
        group_list_view.inited_ = true;
        --group_list_view.uilib_ = ui_utility.uilib();

        local path_str, path_hoder;
        --group_list_view.root_id_ = group_list_view.uilib_:GetControlIDbyCustomID(CID_MAIN_SOCIAL_GROUP_LIST_CONTAINER);

        group_list_view.update_tips_area();

        local path_str,path_hoder = U8toCUIStr("GroupList");
        local group_list_id = group_list_view.uilib_:GetControlIDbyPath(path_str, group_list_view.root_id_);
        GroupBannerList:attach(group_list_view.uilib_, group_list_id);

        local path_guide_str,path_hoder = U8toCUIStr("UserObject_GroupGuide");
        local group_guide_id = group_list_view.uilib_:GetControlIDbyPath(path_guide_str, group_list_view.root_id_);
        group_guide:attach(group_list_view.uilib_, group_guide_id);
        group_guide:show(FALSE);

        --[[local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:get_group_list();]]

        --[[test code
        local report = {};
        report["sender"] = "177788812";
            report["szRemark"] = "test";
            report["nType"] = 0x01;
            report["dwMsg_time"] = "2015-23-34";
        message_box_control.send_friend_report_to_web(report);]]
    end;

    on_im_inited = function()    
        base_utility.log("[group_list_view]on_im_inited");   
        group_list_view.is_im_inited = true;
        --[[if group_list_view.is_ctrl_create == true then
            event_center.send_event(group_event.SHOW_GROUP_LIST, "");
        end]]
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:get_group_list();
    end;

    on_control_create = function(uilib, ctrl_id)
        base_utility.log("[friend_list_view]friend_list on_control_create");
        group_list_view.uilib_ = uilib;
        group_list_view.root_id_ = ctrl_id;
        --[[group_list_view.is_ctrl_create = true;
        if group_list_view.is_im_inited == true then
            event_center.send_event(group_event.SHOW_GROUP_LIST, "");
        end;]]
    end;

    --mini 模式加载完成
    on_mini_mode_load_finish = function()        
        group_list_view.init_ui();
        group_list_view.update_ui();
        comm_center.send_broadcast('i_broadcast_refresh_social_group_tab_reddot', {});
    end;

    init_ui = function()
        group_list_view.show();
    end;

    update_ui = function()
        --获取灰度信息完成否， 
        if group_list_model.is_get_social_test_info == true then
            base_utility.log("[group_list_view]update_ui user level:"..tostring(group_list_model.social_user_level), true);
            --如果是非灰度用户，显示“内测中”
            if group_list_model.social_user_level == 0 then
                group_list_view.show_test_image();
                group_list_view.show_loading("false");
                return;
            end
        else
            base_utility.log("[group_list_view] update_ui group_list_model.is_get_social_test_info = false")
            return;
        end

        group_list_view.update_tips_area();

        if group_list_model.mini_mode_loaded == true then
            -- 界面准备OK
            group_list_view.update_title();

            if group_list_model.is_get_group_list_done == true then

                local group_list = group_list_model.get_group_list();

                if type(group_list) == "table" and #group_list > 0 then
                    GroupBannerList:show_group_list(); 
                    group_guide:show(FALSE);
                    GroupBannerList:show(TRUE);
                else
                    group_guide:show(TRUE);
                    GroupBannerList:show(FALSE);
                end
                group_list_view.show_loading("false");
            end
        end
    end;

    update_title = function()
        if group_list_model.mini_mode_loaded == true then
            local str_group_count = string.format(LoadStr("SOCIAL_GROUP_LIST_MY_GROUP").." (%d)", group_list_model.get_group_count());
            MainCommon.set_static_text(group_list_view.uilib_, group_list_view.root_id_, "Container_GroupInfo\\Static_GroupInfo", str_group_count);
        end
    end;

    update_tips_area = function()
        if login_model.social_user_level == 1 or login_model.social_user_level == 2 then
            if login_model.social_user_level == 1 and login_model.social_invite_code_count > 0 then
                path_str,path_hoder = U8toCUIStr("TinyButton_InviteArea");
                local id = ui_utility.uilib():GetControlIDbyPath(path_str, group_list_view.root_id_);
                if id > 0 then
                    ui_utility.setproperty_by_id(id, "Visible", "true");
                end

                --邀请码个数
                path_str,path_hoder = U8toCUIStr("TinyButton_InviteArea\\TinyButton_InviteCode\\TinyStatic_1"); 
                id = ui_utility.uilib():GetControlIDbyPath(path_str, group_list_view.root_id_);
                if id > 0 then
                    ui_utility.setproperty_by_id(id, "Caption", tostring(login_model.social_invite_code_count));
                end
            else
                path_str,path_hoder = U8toCUIStr("TinyButton_Report");
                local id = ui_utility.uilib():GetControlIDbyPath(path_str, group_list_view.root_id_);
                if id > 0 then
                    ui_utility.setproperty_by_id(id, "Visible", "true");
                end
            end
        end

    end;

    show_loading = function(should_show)
        base_utility.log("[group_list_view]show_loading:"..tostring(should_show), true);
        local path_str,path_hoder = U8toCUIStr("TinyContainer_Loading");
        local id = ui_utility.uilib():GetControlIDbyPath(path_str, group_list_view.root_id_);
        if id ~= nil and id ~= 0 then
           ui_utility.setproperty_by_id(id, "Visible", should_show);
        end
    end;

    --灰度图片显示
    show_test_image = function()
        local path_str,path_hoder = U8toCUIStr("TinyContainer_Test");
        local id = ui_utility.uilib():GetControlIDbyPath(path_str, group_list_view.root_id_);
        if id > 0 then
            base_utility.log("[group_list_view][show_test_image] user level:"..tostring(group_list_model.social_user_level), true);
            ui_utility.setproperty_by_id(id, "Visible", "true");
            group_list_view.show_loading("false");
        end
    end;

    on_search_clicked = function(uilib, ctrl_id)
        if login_model.im_login_infomation == nil then
            return;
        end

        local btn =ui_utility.get_ctrl_ptr_by_id(ctrl_id);
        local rc = btn:GetRectEx(TRUE,TRUE);
        local param = {x = rc.right, y = rc.bottom};
        event_center.send_event(group_event.SHOW_GROUP_SEARCH_MENU,base_utility.table_to_json(param));
        comm_center.call_service('i_service_tqos_group_search_menu_click', {});
    end;

    on_click_on_find_group = function(uilib, ctrl_id)
        local param = {url = "/sociality/search.html?tab=0"};
        event_center.send_event(group_event.SHOW_GROUP_SEARCH_DIALOG,base_utility.table_to_json(param));  


        comm_center.call_service("i_service_tqos_guide_find_group_click", {}); 
    end;


    get_grouplist_done = function(event_id, json_str)
        --group_list_view.show_loading("false");

        group_list_model.is_get_group_list_done = true;

        local group_table = base_utility.json_to_table(json_str)
        if group_table == nil then
            return;
        end

        base_utility.log(string.format("[get_grouplist_done] group count = %d", group_table.count));

        if group_table.count > 0 then
            --GroupBannerList:show(TRUE);
            --group_guide:show(FALSE);
           -- group_list_view.show_loading("false");
            group_list_model.set_group_list(group_table.groups);
            --GroupBannerList:show_group_list();
        else
            local groups = {};
            group_list_model.set_group_list(groups);
            --GroupBannerList:show(FALSE);
            --group_guide:show(TRUE);
        end

        --local str_group_count = string.format("我的群组 (%d)", group_list_model.get_group_count());
        --MainCommon.set_static_text(group_list_view.uilib_, group_list_view.root_id_, "Container_GroupInfo\\Static_GroupInfo", str_group_count);

        group_list_view.update_ui();

        --get group unread message num
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local groups = group_list_model.get_group_list();
        local group_list = {};
        for i_group = 1, #groups do
            table.insert(group_list, groups[i_group].group_id);
        end

        local group_list_param = {};
        group_list_param.id_list = group_list;

        social_network:get_group_unread_msg_num(base_utility.table_to_json(group_list_param));
        --group_list_model.has_get_unread_msg_count = true;
    end;

    on_get_group_unreaed_message_count = function(event_id, json_str)
        group_list_model.has_get_unread_msg_count = true;
        local table_param = base_utility.json_to_table(json_str);
        if table_param == nil then
            return;
        end

        if table_param.result ~= 0 then
            return;
        end

        unread_msg = table_param.groups;
        if unread_msg == nil then
            return;
        end

        GroupBannerList:on_get_group_unreaed_message_count(unread_msg);
    end;


    on_create_group = function(event_id, json_str)
        local group_info = base_utility.json_to_table(json_str);
    
        if group_info == nil then
            return;
        end
        
        if group_info.result ~= 0 then
            return;
        end;
        
        --GroupBannerList:show(TRUE);
        --group_guide:show(FALSE);
        --group_list_view.show_loading("false");

        group_info.newly_create = true;
        group_list_model.add_group_info(1, group_info);
        GroupBannerList:insert_group(0, group_info);
        GroupBannerList:re_sort_group();

        --local str_group_count = string.format("我的群组 (%d)", group_list_model.get_group_count());
        --MainCommon.set_static_text(group_list_view.uilib_, group_list_view.root_id_, "Container_GroupInfo\\Static_GroupInfo", str_group_count);
        group_list_view.update_ui();
    end;

    on_join_group = function(event_id, json_str)

    end;

    on_quit_group = function(event_id, json_str)
        local table_param = base_utility.json_to_table(json_str);

        if table_param.result ~= 0 then
            return;
        end

        group_list_view.remove_group_adapter(table_param.group_id);


        --[[local str_group_count = string.format("我的群组 (%d)", group_list_model.get_group_count());
        MainCommon.set_static_text(group_list_view.uilib_, group_list_view.root_id_, "Container_GroupInfo\\Static_GroupInfo", str_group_count);

        if group_list_model.get_group_count() == 0 then
            GroupBannerList:show(FALSE);
            group_guide:show(TRUE);
        end]]
        group_list_view.update_ui();
    end;

    on_delete_group = function(event_id, json_str)
        local table_param = base_utility.json_to_table(json_str);

        if table_param.result ~= 0 then
            return;
        end

        --GroupBannerList:remove_group(table_param.group_id);  
        --GroupBannerList:deal_with_delete_group_with_message_not_belong_here(table_param.group_id);
        group_list_view.remove_group_adapter(table_param.group_id);

        group_list_view.update_ui();
        --local str_group_count = string.format("我的群组 (%d)", group_list_model.get_group_count());
        --MainCommon.set_static_text(group_list_view.uilib_, group_list_view.root_id_, "Container_GroupInfo\\Static_GroupInfo", str_group_count);
        

        --[[if group_list_model.get_group_count() == 0 then
            GroupBannerList:show(FALSE);
            group_guide:show(TRUE);
        end  ]]       
    end; 

    on_recv_group_report = function(event_id, json_str)
        local table_param = base_utility.json_to_table(json_str);
        if (table_param.type == 0x4) or (table_param.type == 0x5) or (table_param.type == 0x8) or (table_param.type == 0xb)  then
            --GroupBannerList:remove_group(table_param.group_id); 
            --GroupBannerList:deal_with_delete_group_with_message_not_belong_here(table_param.group_id);
            group_list_view.remove_group_adapter(table_param.group_id);

            --local str_group_count = string.format("我的群组 (%d)", group_list_model.get_group_count());
            --MainCommon.set_static_text(group_list_view.uilib_, group_list_view.root_id_, "Container_GroupInfo\\Static_GroupInfo", str_group_count);  
            group_list_view.update_ui();

            talk_dialog_control.on_delete_group_other(table_param.group_id);          

            --[[if group_list_model.get_group_count() == 0 then
                GroupBannerList:show(FALSE);
                group_guide:show(TRUE);
            end      ]]
        elseif (table_param.type > 0x80) and (table_param.type < 0x90) then
            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");

            local group_list = {};
            group_list["id_list"] = {};
            table.insert(group_list["id_list"], table_param.group_id);
            social_network:get_group_info(base_utility.table_to_json(group_list));         
        end
    end;

    on_add_group_accepted = function(event_id, json_str)
        local group_info = base_utility.json_to_table(json_str);

        if group_info == nil then
            return;
        end
        
        --GroupBannerList:show(TRUE);
        --group_guide:show(FALSE);   
        --group_list_view.show_loading("false"); 

        local group_old = group_list_model.get_group_info(group_info.group_id);
        if group_old ~= nil then 
            group_list_model.update_group_info(group_info);
        else
            group_list_model.add_group_info(1, group_info);
            GroupBannerList:insert_group(0, group_info);
            GroupBannerList:deal_with_newly_group_with_message_not_belong_here(group_info.group_id)  
        end

        GroupBannerList:re_sort_group();

        --local str_group_count = string.format("我的群组 (%d)", group_list_model.get_group_count());
        --MainCommon.set_static_text(group_list_view.uilib_, group_list_view.root_id_, "Container_GroupInfo\\Static_GroupInfo", str_group_count);   
        group_list_view.update_ui();
    end;  

    on_add_group_invited = function(event_id, json_str)
        local group_info = base_utility.json_to_table(json_str);

        if group_info == nil then
            return;
        end
        
        --GroupBannerList:show(TRUE);
        --group_guide:show(FALSE);  
        --group_list_view.show_loading("false");  

        local group_old = group_list_model.get_group_info(group_info.group_id);
        if group_old ~= nil then 
            group_list_model.update_group_info(group_info);
        else
            group_list_model.add_group_info(1, group_info);
            GroupBannerList:insert_group(0, group_info);
            GroupBannerList:deal_with_newly_group_with_message_not_belong_here(group_info.group_id)
        end

        GroupBannerList:re_sort_group();

        --local str_group_count = string.format("我的群组 (%d)", group_list_model.get_group_count());
        --MainCommon.set_static_text(group_list_view.uilib_, group_list_view.root_id_, "Container_GroupInfo\\Static_GroupInfo", str_group_count);   
        group_list_view.update_ui();
    end;    

    on_modify_group_info = function(event_id, json_str)
        local group_info = base_utility.json_to_table(json_str);

        if group_info == nil then
            return;
        end

        --通过group report来统一更新
        --group_list_model.update_group_info(group_info);
    end;    

    --大界面迁移做的适配
    remove_group_adapter = function(group_id)
        if group_list_model.mini_mode_loaded == true then
            GroupBannerList:remove_group(group_id);
            GroupBannerList:deal_with_delete_group_with_message_not_belong_here(group_id);
        else
            -- GroupBannerList:remove_group 中的内容
            group_list_model.remove_group_info_by_id(group_id);
            group_message_model.clear_group_messages(group_id);

            local param = {};
            param["group_id"] = group_id;
            event_center.send_event(group_event.REMOVE_GROUP_FROM_GROUP_LIST,base_utility.table_to_json(param));
        end
    end;
}

